package NWish.properties.object3ds
{
	public class MeshProperties extends EntityProperties
	{
		public function MeshProperties(parent)
		{
			super(parent);
			
			var keys
			= 
				['mouseDetails'
				,'castsShadows'
				,new MaterialProperties(parent)
				,new SubMeshProperties(parent)
				,new AnimatorProperties(parent)
				];
			
			addPropertieKeys('Mesh',keys);
			
			
			/*var keysRead
			= 
				['assetType'
				];
			addPropertieKeys_read('Mesh',keysRead);*/
		}
		
		
		override public function newType(scene,dict)
		{
			if(scene == null && dict == null)
				return new type();
			else if(scene)
				return new type();
			else
				throw new Error();
		}
		
	}
}


import NWish.Interface.ILogger;

import NWish.json.OrderDict;
import NWish.properties.Properties;
import NWish.properties.object3ds.MaterialProperties;

import away3d.containers.Scene3D;
import away3d.core.base.SubMesh;
import away3d.entities.Mesh;


class SubMeshProperties
{
	private var _parent;
	
	public function SubMeshProperties(parent)
	{
		_parent = parent;
	}
	
	public function propertiesToDict(obj:Mesh,dict:OrderDict,read_only,saving = false)
	{
		if(read_only == false)
		{
			var subs:Vector.<SubMesh> = obj.subMeshes;
			
			var subs_obj = new OrderDict();
			dict.push('subMeshs', subs_obj);
			
			subs_obj.push(Properties.readMark + 'sub Mesh num', subs.length);
			
			var index = 0;
			for each(var sub:SubMesh in subs)
			{
				var s_obj = new OrderDict();
				
				subs_obj.push('Sub Mesh:' + index, s_obj);
				
				s_obj.push('index',index);
				s_obj.push('name',sub.name);
				s_obj.push('visible',sub.visible);
				
				if(saving == false)
					s_obj.push(Properties.readMark + 'numTriangles',sub.numTriangles);
				
				
				//
				if(subs.length > 1)
				{
					if(sub.material == null)
					{
						s_obj.push('material',null);
					}
					else
					{
						new MaterialProperties(_parent).propertiesToDict(sub,s_obj,read_only,saving)
					}
				}
				
				//
				++index;
			}
		}
		else
		{
			s_obj.push(Properties.readMark + 'numTriangles',sub.numTriangles);
		}
	}
	
	public function dictToProperties(dict,obj:Mesh,scene:Scene3D,isloading)
	{
		try
		{
			var subs_obj = dict['subMeshs'];
			
			if(subs_obj[Properties.readMark + 'sub Mesh num'] <= 1)
				return;
			
			for(var k in subs_obj)
			{
				if(k == Properties.readMark + 'sub Mesh num')
				{
					
				}
				else
				{
					var so = subs_obj[k];
					
					var index = so['index'];
					
					var sub = obj.subMeshes[index];
					sub.visible = so['visible'];
					sub.name = so['name'];
					
					//
					new MaterialProperties(_parent).dictToProperties(so,sub,scene,isloading)
				}
				
				//trace(k,subs_obj[k]);
			}
		}
		catch(e)
		{
			ILogger.log('read json error : ' + e);
		}
	}
	
}




